<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2017/3/31
 * Time: 17:10
 */
namespace V6;
class C_BillList extends \ResourceClass
{
    public $isActive = true;
    public $remark = '获取对账单信息';

    public function get(array $req, array $post, &$result)
    {
        try {
            if (!isset($req['uid'])) {
                return 'require user id ';
            }
            if (!isset($req['userType'])) {
                return 'require user type';
            }
            if (!isset($req['k']) || !isset($req['t'])) {
                return 'data error';
            }

            $customer_handler = new \Biz\UserHandler();
            $customer_cls = $customer_handler->getById($req['userType'], $req['uid']);
            if (!is_object($customer_cls) && $customer_cls->getEnterpriseId() == false) {
                return 'data error';
            }
            $enterprise_id = $customer_cls->getEnterpriseId();
            /*if (\Biz\UserHandler::checkAuth($req['userType'], $req['uid'], $enterprise_id, $req['id'] . $req['t'], $req['k']) == false) {
                return 'data illegal';
            }*/

            if (!array_key_exists('uid', $req) || empty($req['uid'])) {
                return 'require uid';//买家id
            }
            if (!array_key_exists('channelId', $req) || empty($req['channelId'])) {
                return 'require channelId';//渠道id
            }

            //分页信息
            $l = isset($req['l']) ? $req['l'] : 20;
            $o = isset($req['o']) ? $req['o'] : 0;

            // 实例化列表类
            $tabulation = new \Cls\Tabulation();

            //获取时间节点
            $time_start = date("Y-m-d", strtotime("-1 month"));//当前时间前一个月时间
            $time_end = date('Y-m-d', time());//当前时间

            //有查询条件，拼接参数信息
            if (!isset($req['s_time']) && !isset($req['e_time']) && !isset($req['typeId']) && !isset($req['payType'])) {
                //TODO 后续添加筛选条件获取对账单信息

            } else {//无查询条件，默认显示截止当前时间之前一个月信息
                $tabulation->setWhere('customer_id', $req['uid'])
                    ->setWhere('enterprise_channel_id', $req['channelId'])
                    ->setWhere('enterprise_id', $enterprise_id)
                    ->setWhere('order_time', '>=', $time_start . ' 00:00:00')
                    ->setWhere('order_time', '<=', $time_end . ' 23:59:59')
                    ->setOffset($o)
                    ->setLimit($l)
                    ->setOrderBy('id', 'desc');
            }

            $bill_handler = new \Biz\BillHandler();

            //查询条件信息
            $searchList = array(
                'bill_period' => \Cls\Bill\Bill::BILL_PERIOD,
                'bill_type' => \Cls\Bill\Bill::BILL_TYPE,
                'bill_payment' => \Cls\Bill\Bill::BILL_PAYMENT
            );

            //账单类型信息
            $billTypeList = array(
                '0' => array("type_id" => \Cls\Bill\Bill::TYPE_ID_FK, "name" => \Cls\Bill\Bill::$BillStatus[\Cls\Bill\Bill::TYPE_ID_FK]),
                '1' => array("type_id" => \Cls\Bill\Bill::TYPE_ID_TK, "name" => \Cls\Bill\Bill::$BillStatus[\Cls\Bill\Bill::TYPE_ID_TK])
            );
            //支付方式信息
            $billPaymentList = array(
                '0' => array("payment_code" => \Cls\Bill\Bill::PAYMENT_CODE_XXZF, "name" => \Cls\Bill\Bill::$BillPayStatus[\Cls\Bill\Bill::PAYMENT_CODE_XXZF]),
                '1' => array("payment_code" => \Cls\Bill\Bill::PAYMENT_CODE_ZFB, "name" => \Cls\Bill\Bill::$BillPayStatus[\Cls\Bill\Bill::PAYMENT_CODE_ZFB])
            );

            //获取对账单汇总信息
            $time_start = $time_start . ' 00:00:00';
            $time_end = $time_end . ' 23:59:59';
            $orderInfo = $bill_handler->getOrderInfo($req['uid'], $req['channelId'], $req['enterpriseId'], $time_start, $time_end);
            $refundInfo = $bill_handler->getRefundOrderInfo($req['uid'], $req['channelId'], $req['enterpriseId'], $time_start, $time_end);
            $billStatistics = array(
                'bill_start_time' => $time_start,
                'bill_end_time' => $time_end,
                'order_total_price' => !empty($orderInfo['order_total_price']) ? $orderInfo['order_total_price'] : 0.00,
                'order_count' => !empty($orderInfo['order_count']) ? $orderInfo['order_count'] : 0,
                'chargeback_total_price' => !empty($refundInfo['chargeback_total_price']) ? $refundInfo['chargeback_total_price'] : 0.00,
                'chargeback_count' => !empty($refundInfo['chargeback_count']) ? $refundInfo['chargeback_count'] : 0
            );

            //获取对账单列表信息
            $bill_handler->getBillListByEnterpriseAndCustomerAndChannel($tabulation);
            $items = $itemsOrder = $itemsRefund = $items_order_info = $items_refund_info = array();
            if ($tabulation->getSuccessStatus()) {
                foreach ($tabulation->getData() as $bill_cls) {
                    if ($bill_cls->getOrderType() == 1) {//订单数据
                        $itemsOrder['ordercode'] = $bill_cls->getOrderCode();
                        $itemsOrder['type'] = \Cls\Bill\Bill::$BillStatus[1];
                        $itemsOrder['amount'] = '+' . $bill_cls->getOrderPrice();
                        $itemsOrder['platform'] = $bill_cls->getPlatform() == 0 ? \Cls\Bill\Bill::$BillPayStatus[1] : \Cls\Bill\Bill::$BillPayStatus[2];
                        $itemsOrder['order_time'] = date('Y-m-d', strtotime($bill_cls->getOrderTime()));
                        $items_order_info[] = $itemsOrder;
                    }
                    if ($bill_cls->getOrderRefundType() == 1) {//退款数据
                        $itemsRefund['ordercode'] = $bill_cls->getRefundCode();
                        $itemsRefund['type'] = \Cls\Bill\Bill::$BillStatus[2];
                        $itemsRefund['amount'] = '-' . $bill_cls->getRefundPrice();
                        $itemsRefund['platform'] = $bill_cls->getPlatform() == 1 ? \Cls\Bill\Bill::$BillPayStatus[2] : \Cls\Bill\Bill::$BillPayStatus[1];
                        $itemsRefund['order_time'] = date('Y-m-d', strtotime($bill_cls->getOrderTime()));
                        $items_refund_info[] = $itemsRefund;
                    }
                }
                $items = array_merge($items_order_info, $items_refund_info);
            }
            //结果集赋值
            $result['SearchList'] = $searchList;
            $result['BillTypeList'] = $billTypeList;
            $result['BillPaymentList'] = $billPaymentList;
            $result['BillStatistics'] = $billStatistics;
            $result['BillItems'] = $items;
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }
}